#ifndef NADJUSTEDWIN_H
#define NADJUSTEDWIN_H
#include <QtWidgets>
#include "solar/trunk/NbaseWin.h"
#include "solar/trunk/NSurveyor.h"

class NAdjustedWin : public NMainWindow{

    Q_OBJECT

public:

    enum ValueChangeWay{
        ADDTION,
        SUBTRACTION,
        MULTIPLICATION,
        DIVISION
    };

    NAdjustedWin(QWidget *parent=nullptr, bool _autoExit=false) : autoExit(_autoExit), changevalue(0), changeway(ValueChangeWay::ADDTION){
        setParent(parent);
        setAttribute(Qt::WA_DeleteOnClose);
        setCentralWidget(prograss);

        winmvAndstretchOff();
        d_margin_radius(QMargins(3,0,3,0), 4);
        d_filler_enable(NTitleBar::BACKCOLOR, true);
        d_filler_color(QColor(242,241,247));
        captionBar()->setFixedHeight(28);
        captionBar()->d_margin_radius(QMargins(3,0,3,0), 4);
        captionBar()->d_filler_enable(NTitleBar::BACKCOLOR, true);
        captionBar()->d_filler_color(QColor(242,241,247));
        captionBar()->d_captionico(":/PIXMAP/bench/caption-3Logo-dark.png", 1, 2);
        captionBar()->d_basicbuttom_configur(NT::PIXLOAD_IGNORERATIO_LOWER, NT::DRAWTYPE_ROUND, 3, 1.2);
        captionBar()->d_basicbuttom_where({NTitleBar::Rroof_Normal,
                                           NTitleBar::Rroof_Min,
                                           NTitleBar::Rroof_Max,
                                           NTitleBar::Rroof_Exit},
                                           false);
        prograss->setValue(false);
        autoTimer.setInterval(1430);
        captionBar()->d_caption(QString::number(0), QFont("KaiTi_GB2312", 9), QColor(170,92,200));
        connect(&autoTimer, &QTimer::timeout, this, &NAdjustedWin::close);
        connect(prograss, &NProcess::positionChange, this, &NAdjustedWin::valuechange);
    }

    void changeValue(ValueChangeWay way, double value){
        changevalue = value;
        changeway = way;
    }

    void setPrograssAttribute(double minnum, double maxnum, double step){
        prograss->setPrograssAttribute(minnum, maxnum, step);
    }

    void sico(QString curtion){
        captionBar()->d_captionico(curtion);
    }

    void setAutoExit(bool enable){
        autoExit = enable;
    }

    void setValue(int initValue){
        captionBar()->d_caption(QString::number(initValue));
        prograss->setValue((double)initValue);
    }

private:
    bool autoExit;
    QTimer autoTimer;
    double changevalue;
    ValueChangeWay changeway;
    NProcess *prograss = new NProcess(this);

    void leaveEvent(QEvent *){
        if (!autoTimer.isActive() && autoExit) autoTimer.start();
    }

    void enterEvent(QEnterEvent *){
        if (autoTimer.isActive() && autoExit) autoTimer.stop();
    }

    void changeEvent(QEvent *event){
        if (event->type() == QEvent::EnabledChange) {prograss = 0;}
        QWidget::changeEvent(event);
    }

    void showEvent(QShowEvent *event){
        if (!autoTimer.isActive() && autoExit) autoTimer.start();
        QWidget::changeEvent(event);
    }

    ~NAdjustedWin(){
        delete prograss;
    }

    void valuechange(double value){
        double newvalue = 0;
        switch (changeway)
        {
            case ADDTION:
               newvalue = value + changevalue;
                break;
            case SUBTRACTION:
                newvalue = value - changevalue;
                break;
            case MULTIPLICATION:
                newvalue = value * changevalue;
                break;
            case DIVISION:
                newvalue = value / changevalue;
                break;
        }
        emit positionChange(newvalue);
        captionBar()->d_caption(QString::number(newvalue));
    }

signals:
    void positionChange(double prograss);
};

#endif
